package com.boxing;

import com.boxing.beans.Result;
import com.boxing.beans.UserInfo;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;

public class Register{
    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 解析 urlEncode 格式的数据
//        String user = req.getParameter("user");
//        String email = req.getParameter("email");
//        String psd = req.getParameter("psd");
//        System.out.println("注册收到的参数"+ user+ email+psd);

        // 解析 json 格式的数据
        ServletInputStream inputStream = req.getInputStream();
        byte[] bytes = inputStream.readAllBytes();
        String str = new String(bytes, "utf-8");
        System.out.println("服务器收到请求体中的数据："+str);
        // 解析 json 使用 gson 库解析json
        Gson gson = new Gson();
        UserInfo userInfo = gson.fromJson(str, UserInfo.class);
        System.out.println("服务器收到请求体中的数据:" + userInfo);
//        resp.sendRedirect("/webDemo/login.html");
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        result.setCode(200);
        result.setMsg("注册成功");
        String outStr = gson.toJson(result);
        out.print(outStr);
    }
}
